package windowFactoryPackage;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JOptionPane;
import java.util.Calendar;
import java.text.SimpleDateFormat;

public class Insert {

	public boolean createUser() {
		boolean flag = false;
		
		try {
			String date = AccountPopUp.jtfCreditExpYear.getText() + "-" + AccountPopUp.jtfCreditExpMonth.getText() + "-01";
			// Step 1: Load the JDBC driver.
			Class.forName("oracle.jdbc.driver.OracleDriver");
			// Step 2: Establish the connection to the database.
			String url = "jdbc:oracle:thin:@141.165.201.104:1521:cit10g2";
			Connection conn = DriverManager.getConnection(url, "student63",
					Credentials.pass);
			Statement stmt = conn.createStatement();

			ResultSet rs = stmt
					.executeQuery("SELECT User_ID FROM RSA$UserData WHERE LOWER(User_ID) LIKE LOWER('"
							+ AccountPopUp.jtfUser_ID.getText() + "')");

			if (rs.next() == true) {
				JOptionPane.showMessageDialog(null, "Account Taken!");
				System.out.println("The Username is taken!");
			} else {
				// allows password field for password, turning result into
				// string
				String pass = "";
				String rePass = "";
				char[] passChar = AccountPopUp.jpfUserPassword.getPassword();
				char[] rePassChar = AccountPopUp.jpfRetypePass.getPassword();
				for (int i = 0; i < passChar.length; i++) {
					pass += passChar[i];
				}
				for (int i = 0; i < rePassChar.length; i++) {
					rePass += rePassChar[i];
				}
				
				//turn 3 textfields for phone# into 1 string of 10 digits
				String phoneNum = "" + AccountPopUp.jtfPhoneArea.getText() + AccountPopUp.jtfPhonePrefix.getText() + AccountPopUp.jtfPhoneLine.getText();

				// get current date in YYYY-MMM-DD format to insert as UserSince
				final String DATE_FORMAT = "yyyy-MMM-dd";
				Calendar cal = Calendar.getInstance();
				SimpleDateFormat formatDate = new SimpleDateFormat(DATE_FORMAT);
				String currDate = formatDate.format(cal.getTime());
				System.out.println(currDate);

				// makes sure both password fields are equal before adding to DB
				// && username and pass are more than 5 in size, && addr, email
				// is not empty
				if (pass.equals(rePass)
						&& AccountPopUp.jtfUser_ID.getText().length() > 2
						&& pass.length() > 5
						&& AccountPopUp.jtfUserStreet.getText().length() > 0
						&& AccountPopUp.jtfUserCity.getText().length() > 0
						&& AccountPopUp.jtfUserST.getText().length() == 2
						&& AccountPopUp.jtfUserZip.getText().length() == 5
						&& AccountPopUp.jtfUserEmail.getText().length() > 0
						&& phoneNum.length() == 10
						&& AccountPopUp.jtfUserFirstName.getText().length() > 0
						&& AccountPopUp.jtfUserLastName.getText().length() > 0) {
					//valid entry, enter data then dispose window back on AccountPopUp
					flag = true;
					// populate RSA$UserData with new username when it Doesn't Exist
					// User_ID,UserAddress,UserEmail,UserPhone,Password,UserSince
					stmt.executeUpdate("INSERT INTO RSA$UserData values('"
							+ AccountPopUp.jtfUser_ID.getText().toLowerCase() + "','"
							+ AccountPopUp.jtfUserStreet.getText() + "','"
							+ AccountPopUp.jtfUserEmail.getText() + "','"
							+ phoneNum + "','"
							+ pass + "', TO_DATE( '" 
							+ currDate + "', 'YYYY-MON-DD'),'"
							+ AccountPopUp.jtfUserCity.getText() + "','"
							+ AccountPopUp.jtfUserST.getText().toUpperCase() + "','"
							+ AccountPopUp.jtfUserZip.getText() + "','"
							+ AccountPopUp.jtfUserFirstName.getText() + "','"
							+ AccountPopUp.jtfUserLastName.getText() + "')");
					
					stmt.executeUpdate("INSERT INTO RSA$UserPurchaseInfo values('" + AccountPopUp.jtfUser_ID.getText().toLowerCase() + 
										"','" + AccountPopUp.jtfCreditCardNum.getText() + "','" + AccountPopUp.jtfUserEmail.getText() + 
										"', to_date('" + date + "','YYYY-MON-DD'))");

					JOptionPane.showMessageDialog(null,
							"Account Created Successfully!");
					System.out.println("Account created successfully!");
					conn.close();
				} else {
					if(pass.equals(rePass) == false){
						JOptionPane.showMessageDialog(null,
						"Passwords DO NOT Match!");
						System.out.println("Password don't match!");
					}
					if(AccountPopUp.jtfUser_ID.getText().length() <= 2){
						JOptionPane.showMessageDialog(null,
						"User Name must be 2-20 characters!");
						System.out.println("Need longer User Name");
					}
					if(pass.length() <= 5){
						JOptionPane.showMessageDialog(null,
						"Password too short!\nEnter password between 6-20 characters.");
						System.out.println("Need longer password");
					}
					if(AccountPopUp.jtfUserEmail.getText().length() <= 0){
						JOptionPane.showMessageDialog(null,
						"Must enter valid email address!");
						System.out.println("Need email");
					}
					if(AccountPopUp.jtfUserStreet.getText().length() <= 0){
						JOptionPane.showMessageDialog(null,
						"Must enter street name!");
						System.out.println("Need street name");
					}
					if(AccountPopUp.jtfUserCity.getText().length() <= 0){
						JOptionPane.showMessageDialog(null,
						"Must enter city name!");
						System.out.println("Need city name");
					}
					if(AccountPopUp.jtfUserST.getText().length() != 2){
						JOptionPane.showMessageDialog(null,
						"State must be 2 character state code!");
						System.out.println("Need state code");
					}
					if(AccountPopUp.jtfUserZip.getText().length() != 5){
						JOptionPane.showMessageDialog(null,
						"Must use 5-digit Zip Code!");
						System.out.println("Need zip code");
					}
					if(phoneNum.length() != 10){
						JOptionPane.showMessageDialog(null,
						"Phone number must be 10 digits!");
						System.out.println("Invalid 10-digit phone#");
					}
				}

			}

		} catch (Exception ex) {
			System.err.println("Got an exception! ");
			System.err.println(ex.getMessage());

		}

		return flag;
	}

}